package com.sz.app.oss.api.common.customer;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.common.customer.entity.CusBusinessContacts;
import com.sz.biz.common.customer.service.CustomerBusinessContactsService;
import com.sz.biz.common.customer.dto.CusBusinessContactsDto;
import com.sz.common.base.dto.CodeNameDto;
import com.sz.common.base.dto.QResultDto;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by xutao on 17/4/21.
 */
@RestController
@RequestMapping("${api}/customer/business")
@Api(description = " ", tags = "037-005、客户信息管理-业务联系人")
public class CusBusinessContactsController extends BaseController {

    @Autowired
    CustomerBusinessContactsService customerBusinessContactsService;

    /**
     * 搜索客户业务联系人
     *
     * @throws BizException ex
     */
    @ApiOperation(value = "搜索客户业务联系人", notes = "搜索客户业务联系人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询的关键字",
                    dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sortName", value = "查询的关键字",
                    dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "isAsc", value = "排序类型,true为升序，false为降序", required = false,
                    dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页 默认第一页",
                    dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小",
                    dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "refCustomerId", value = "客户关联id",
                    dataType = "int", paramType = "query")})
    @RequiresPermissions(value = {Permissions.CUSTOMER_LOGISTICS_MGT,Permissions.CUSTOMER_TRADE_MGT},
            logical = Logical.OR)
    @RequestMapping(value = "/search", method = {RequestMethod.GET})
    public QResultDto getBusinesContacts(@RequestParam(defaultValue = "") String q,
                                         @RequestParam(defaultValue = "refBussContactId") String sortName,
                                         @RequestParam(required = false, defaultValue = "true") boolean isAsc,
                                         @RequestParam(defaultValue = "1") int pageIndex,
                                         @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                         @RequestParam int refCustomerId) throws BizException {

        return customerBusinessContactsService.getContactList(q, sortName, isAsc, pageIndex, pageSize, refCustomerId);
    }

    /**
     * 新建一个客户业务联系人
     *
     * @throws Exception ex
     */
    @ApiOperation(value = "新建客户业务联系人", notes = "-<br>权限：" +
            Permissions.CUSTOMER_LOGISTICS_MGT + " or " +Permissions.CUSTOMER_TRADE_MGT)
    @RequiresPermissions(value = {Permissions.CUSTOMER_LOGISTICS_MGT,Permissions.CUSTOMER_TRADE_MGT},
            logical = Logical.OR)
    @RequestMapping(value = "", method = {RequestMethod.POST})
    public ResultDto saveBusinesContacts(@RequestBody @Validated CusBusinessContacts cusBusinessContacts) throws Exception {

        int businessContactId = customerBusinessContactsService.save(cusBusinessContacts);

        ResultDto dto = getSaveSuccessDto(MessagesCodes.MODULE_CUSTOMER_BUSINESS_CONTACTS);

        dto.setData(businessContactId);

        return dto;
    }

    /**
     * 批量删除客户业务联系人
     *
     * @throws Exception ex
     */
    @ApiOperation(value = "批量删除客户业务联系人", notes = "非物理删除 打标志 不允许登录<br>权限：" +
            Permissions.CUSTOMER_LOGISTICS_MGT + " or " +Permissions.CUSTOMER_TRADE_MGT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "客户业务联系人ids",
                    required = true, dataType = "string", paramType = "body")})
    @RequiresPermissions(value = {Permissions.CUSTOMER_LOGISTICS_MGT,Permissions.CUSTOMER_TRADE_MGT},
            logical = Logical.OR)
    @RequestMapping(value = "/deletions", method = {RequestMethod.POST})
    public ResultDto deleteBusinesContacts(@RequestBody List<Integer> ids) throws Exception {
        customerBusinessContactsService.delete(ids);
        return getDeleteSuccessDto(MessagesCodes.MODULE_CUSTOMER_BUSINESS_CONTACTS);
    }

    /**
     * 修改客户业务联系人
     *
     * @throws Exception ex
     */
    @ApiOperation(value = "修改客户业务联系人", notes = "-<br>权限：" +
            Permissions.CUSTOMER_LOGISTICS_MGT + " or " +Permissions.CUSTOMER_TRADE_MGT)
    @RequiresPermissions(value = {Permissions.CUSTOMER_LOGISTICS_MGT,Permissions.CUSTOMER_TRADE_MGT},
            logical = Logical.OR)
    @RequestMapping(value = "", method = {RequestMethod.PUT})
    public ResultDto updateBusinesContacts(@RequestBody @Validated CusBusinessContacts cusBusinessContacts) throws Exception {

        customerBusinessContactsService.updateById(cusBusinessContacts);

        ResultDto dto = getUpdateSuccessDto(MessagesCodes.MODULE_CUSTOMER_BUSINESS_CONTACTS);

        dto.setData(cusBusinessContacts.getId());

        return dto;
    }

    /**
     * 凭借主账号Id查询业务联系人
     *
     * @throws Exception ex
     */
    @ApiOperation(value = "凭借主账号Id查询业务联系人", notes = "-<br>权限：" +
            Permissions.CUSTOMER_LOGISTICS_MGT + " or " +Permissions.CUSTOMER_TRADE_MGT)
    @RequiresPermissions(value = {Permissions.CUSTOMER_LOGISTICS_MGT,Permissions.CUSTOMER_TRADE_MGT},
            logical = Logical.OR)
    @RequestMapping(value = "/customers/{customerId}", method = {RequestMethod.GET})
    public QResultDto findBusinesContactsByCustomerId(@PathVariable int customerId) throws Exception {

        List<CusBusinessContacts> cusBusinessContactsList = customerBusinessContactsService.findByCustomerId(customerId);

        QResultDto qResultDto = new QResultDto();

        qResultDto.setData(cusBusinessContactsList);

        return qResultDto;
    }

    /**
     * 凭借业务联系人Id查询业务联系人详细信息
     *
     * @throws Exception ex
     */
    @ApiOperation(value = "凭借业务联系人Id查询业务联系人详细信息", notes = "-<br>权限：" +
            Permissions.CUSTOMER_LOGISTICS_MGT + " or " +Permissions.CUSTOMER_TRADE_MGT)
    @RequiresPermissions(value = {Permissions.CUSTOMER_LOGISTICS_MGT,Permissions.CUSTOMER_TRADE_MGT},
            logical = Logical.OR)
    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    public QResultDto findBusinesContactsById(@PathVariable int id) throws Exception {
        CusBusinessContactsDto cusBusinessContacts = customerBusinessContactsService.findById(id);
        QResultDto qResultDto = new QResultDto();
        qResultDto.setData(cusBusinessContacts);

        return qResultDto;
    }

    /**
     * 业务联系人是否重复添加
     *
     * @throws Exception ex
     */
    @ApiOperation(value = "业务联系人是否重复添加(异步校验接口)", notes = "-<br>权限：" +
            Permissions.CUSTOMER_LOGISTICS_MGT + " or " +Permissions.CUSTOMER_TRADE_MGT)
    @RequiresPermissions(value = {Permissions.CUSTOMER_LOGISTICS_MGT,Permissions.CUSTOMER_TRADE_MGT},
            logical = Logical.OR)
    @RequestMapping(value = "/exist", method = {RequestMethod.POST})
    public ResultDto checkContactsExist(@RequestBody CusBusinessContacts cusBusinessContacts) throws Exception {

        Boolean isExist = customerBusinessContactsService.checkContactsExist(cusBusinessContacts);

        ResultDto dto = new ResultDto();

        dto.setData(isExist);

        return dto;
    }

    /**
     * 获取所有的业务联系人组信息
     */
    /**
     * 业务联系人是否重复添加
     *
     * @throws Exception ex
     */
    @ApiOperation(value = "获取所有的业务联系人组信息", notes = "-<br>权限：" +
            Permissions.CUSTOMER_LOGISTICS_MGT + " or " +Permissions.CUSTOMER_TRADE_MGT)
    @RequiresPermissions(value = {Permissions.CUSTOMER_LOGISTICS_MGT,Permissions.CUSTOMER_TRADE_MGT},
            logical = Logical.OR)
    @RequestMapping(value = "/groupinfo", method = {RequestMethod.POST})
    public ResultDto getBusinessContactsGroupList() throws Exception {

        List<CodeNameDto> codeNameDtoList = customerBusinessContactsService.getBusinessContactsGroupList();

        ResultDto dto = new ResultDto();

        dto.setData(codeNameDtoList);

        return dto;
    }
}
